{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典简介2\n",
    "\n",
    "当用于表示更大的相关数据块时，字典就显得特别有用。下面的图片显示了来自名为Jira的票务软件的更真实的“票（ticket）”。\n",
    "\n",
    "让我们来看一看！\n",
    "\n",
    "![Jira Ticket](https://d17h27t6h515a5.cloudfront.net/topher/2017/November/59fb6ccd_jira-ticket/jira-ticket.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The keys for this dictionary are...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dict_keys(['type', 'status', 'priority', 'resolution', 'description', 'attachments', 'people', 'dates'])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Note how the structure of a ticket is captured in the\n",
    "\n",
    "\n",
    "ticket = {\n",
    "    \"type\" : \"bug\",\n",
    "    \"status\": \"done\",\n",
    "    \"priority\": \"medium\",\n",
    "    \"resolution\": \"done\",\n",
    "    \"description\" : \"testing 123\",\n",
    "    \"attachments\" : [],\n",
    "    \"people\": {\n",
    "        \"assignee\" : None,\n",
    "        \"reporter\" : {\n",
    "            \"name\" : \"Andy Brown\",\n",
    "            \"image\": \"www.example_image_url.com\"\n",
    "        },\n",
    "        \"votes\" : 0,\n",
    "        \"watchers\" : [\n",
    "            {\n",
    "                \"name\": \"Andy Brown\",\n",
    "                \"image\": \"www.example_image_url.com\"  \n",
    "            }\n",
    "        ]\n",
    "    },\n",
    "    \"dates\" : {\n",
    "        \"created\" : \"6 days ago\",\n",
    "        \"updated\" : \"6 days ago\",\n",
    "        \"resolved\": \"6 days ago\"\n",
    "    }\n",
    "}\n",
    "\n",
    "# In this example, ticket is a dictionary with the following \"keys\":\n",
    "\n",
    "print(\"The keys for this dictionary are...\")\n",
    "ticket.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Andy Brown'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# notice how nicely the following code reads... \n",
    "\n",
    "ticket['people']['reporter']['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'sample_data'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-361e5e10de89>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# let's pull in a bunch of \"dummy\" data\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msample_data\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mbig_tickets\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrandom\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'sample_data'"
     ]
    }
   ],
   "source": [
    "# let's pull in a bunch of \"dummy\" data\n",
    "\n",
    "from sample_data import big_tickets\n",
    "import random\n",
    "\n",
    "print(\"there are\", len(big_tickets), \"big tickets\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# grab a random ticket and take a look at it\n",
    "\n",
    "random_ticket = random.choice(big_tickets)\n",
    "random_ticket"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TODO -  练习 - 查找所有包含8位以上观众的票\n",
    "\n",
    " 列表和字典可以很好地协同工作。请注意，在上面显示的随机票中，`people`字段代表一个词典。 该字典包含其他数据，包括其关联值为一个列表的一个`watchers`关键字。\n",
    "\n",
    "在本练习中，你将对250张票（每个门票对应一个词典）的列表进行过滤，最后只列出最受欢迎的票（由观看人数定义）的一个较小列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_popular_tickets(tickets):\n",
    "    \"\"\"\n",
    "    From a list of tickets, fetch all the tickets with 8 or \n",
    "    more \"watchers\". \n",
    "    \"\"\"\n",
    "    popular_tickets = []\n",
    "    #\n",
    "    # TODO - your code here\n",
    "    # \n",
    "    return popular_tickets\n",
    "\n",
    "popular = get_popular_tickets(big_tickets)\n",
    "\n",
    "# TESTING CODE \n",
    "assert( len(popular) > 0 ) # must be at least one popular ticket\n",
    "\n",
    "for ticket in popular:\n",
    "    assert( len(ticket['people']['watchers']) >= 8 )\n",
    "    \n",
    "print(\"Nice job! It looks like your function is working.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "\n",
    "#\n",
    "\n",
    "#\n",
    "\n",
    "#\n",
    "\n",
    "#\n",
    "\n",
    "# Spoiler alert! Try to avoid looking at the solution until you've\n",
    "# attempted the problem yourself.\n",
    "\n",
    "#\n",
    "\n",
    "#\n",
    "\n",
    "#\n",
    "def get_popular_tickets_solution(tickets):\n",
    "    \"\"\"\n",
    "    From a list of tickets, fetch all the tickets with 8 or \n",
    "    more \"watchers\". \n",
    "    \"\"\"\n",
    "    popular_tickets = []\n",
    "    for ticket in tickets:\n",
    "        num_watchers = len(ticket['people']['watchers'])\n",
    "        if num_watchers >= 8:\n",
    "            popular_tickets.append(ticket)\n",
    "    return popular_tickets  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
